<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>

<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="stcokLeftDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/csm/buyExpPlan/buyPlanImpExpIndex')">采购对消入出库</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="expBuyPlan" class="form-inline"  onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/csm/buyExpPlan/buyPlanImpExpIndex');">
                                            <div class="form-group">
                                                <label class=" control-label">采购单号：</label>
                                                <form:select path="purchaseNumber" id="purchaseNumber" htmlEscape="false" class="form-control"
                                                              data-parsley-required="true">
                                                    <form:option value="" label=""></form:option>
                                                    <form:options items="${his_fns:getNoImpNum()}" itemLabel="purchaseNumber" itemValue="purchaseNumber" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">入库类别：</label>
                                                <form:select path="expStock.expImportMaster.importClass" id="importClass" htmlEscape="false" class="form-control" maxlength="1"
                                                             onchange="loadSupplier()" data-parsley-required="true">
                                                    <form:options items="${his_fns:getImpOrOutpList('1')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">供货单位：</label>
                                                <form:select path="expStock.expImportMaster.supplier" id="supplier" htmlEscape="false" class="form-control"  data-parsley-required="true">
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">付款方式：</label>
                                                <form:select path="expStock.expImportMaster.accountType" id="accountType" htmlEscape="false" class="form-control" maxlength="1" data-parsley-required="true">
                                                    <form:option value="" label=""></form:option>
                                                    <form:options items="${fns:getDictList('ACCOUNT_TYPE')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                    <input class="btn btn-primary" type="submit" id="getBuyExpPlanData" value="提取" onclick="getBuyPlanList()"/>
                                                    <input id="btnSubmit" class="btn btn-primary" type="button" onclick="savePlanListForm()" value="保存"/>
                                            </div><br/><br />

                                            <div class="form-group">
                                                <label class=" control-label">出库类别：</label>
                                                <form:select path="expStock.expExportMaster.exportClass" id="exportClass" htmlEscape="false" class="form-control" maxlength="1"
                                                             onchange="loadSupplier()" data-parsley-required="true">
                                                    <form:options items="${his_fns:getImpOrOutpList('0')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">日期：</label>
                                                <input name="startDate" id="startDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly
                                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value='<fmt:formatDate value="${sysdate}" pattern="yyyy-MM-dd HH:mm:ss"/>' />
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">合计：</label>
                                                <input name="totalMoney" id="totalMoney" value="${totalMoney}" htmlEscape="false" class="form-control" maxlength="64" />
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <form id="savePlanListForm" action="${ctx}/csm/buyExpPlan/saveBuyPlanImpExp" method="post" class="form-inline">
                                    <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                                        <thead>
                                        <tr>
                                            <th hidden>jsonText</th>
                                            <th hidden>id</th>
                                            <th hidden>采购单号</th>
                                            <th>申请科室</th>
                                            <th hidden>发放科室</th>
                                            <th hidden>infoId</th>
                                            <th hidden>expInfoId</th>
                                            <th hidden>编码</th>
                                            <th>名称</th>
                                            <th hidden>包装数量</th>
                                            <th>规格</th>
                                            <th hidden>单位代码</th>
                                            <th>单位</th>
                                            <th hidden>最小规格</th>
                                            <th hidden>最小单位代码</th>
                                            <th>厂家</th>
                                            <th>进价</th>
                                            <th>批号</th>
                                            <th>有效期</th>
                                            <th>灭菌方式</th>
                                            <th>灭菌批号</th>
                                            <th>灭菌日期</th>
                                            <th>数量</th>
                                            <th>操作</th>
                                            <th hidden>入库类别</th>
                                            <th hidden>供货单位</th>
                                            <th hidden>付款方式</th>
                                            <th hidden>出库类别</th>
                                            <th hidden>入出库日期</th>
                                        </tr>
                                        </thead>
                                        <tbody id="buyPlanBody">
                                        <c:forEach items="${list}" var="expBuyPlan" varStatus="idx">
                                            <tr xia="${idx.index}">
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.id" class="form-control"
                                                        value="${expBuyPlan.expProvideApplication.id}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].purchaseNumber" class="form-control"
                                                        value="${expBuyPlan.purchaseNumber}"></td>
                                                <td> ${his_fns:getExpDeptById(expBuyPlan.expProvideApplication.storageIn)}<input type="hidden"
                                                        name="expBuyPlanList[${idx.index}].expProvideApplication.storageIn" class="form-control"
                                                        value="${expBuyPlan.expProvideApplication.storageIn}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.storageOut"
                                                      class="form-control" value="${expBuyPlan.expProvideApplication.storageOut}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.infoId"
                                                        class="form-control" value="${expBuyPlan.expProvideApplication.infoId}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.expInfoId"
                                                              class="form-control" value="${expBuyPlan.expProvideApplication.expInfoId}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.expCode"
                                                              class="form-control" value="${expBuyPlan.expProvideApplication.expCode}"></td>
                                                <td> ${expBuyPlan.expName}</td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.packageNum"
                                                              class="form-control" value="${expBuyPlan.expProvideApplication.packageNum}"></td>
                                                <td><input type="text" style="width: 60px" name="expBuyPlanList[${idx.index}].packageSpec" readonly
                                                           class="form-control" value="${expBuyPlan.packageSpec}"></td>
                                                <td hidden><input type="text" id="packageUnit" name="expBuyPlanList[${idx.index}].packageUnit"
                                                              class="form-control" value="${expBuyPlan.packageUnit}"></td>
                                                <td>${fns:getDictLabel(expBuyPlan.packageUnit,'SPEC_UNIT','')}</td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.minSpec"
                                                              class="form-control" value="${expBuyPlan.expProvideApplication.minSpec}"></td>
                                                <td hidden><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.minUnits"

                                                              class="form-control" value="${expBuyPlan.expProvideApplication.minUnits}"></td>
                                                <td>${his_fns:getExpSupplierById(expBuyPlan.supplierId)}</td>
                                                <td><input type="text" style="width: 60px" name="expBuyPlanList[${idx.index}].expPrice.tradePrice" id="tradePrice${idx.index}"
                                                              class="form-control" value="${expBuyPlan.expPrice.tradePrice}"></td>
                                                <td><input type="text" name="expBuyPlanList[${idx.index}].expStock.batchNo" style="width:120px"
                                                              class="form-control" value="${expBuyPlan.expStock.batchNo}"></td>
                                                <td><input name="expBuyPlanList[${idx.index}].expStock.expireDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true"
                                                           readonly onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value='<fmt:formatDate value="${expBuyPlan.expStock.expireDate}"
                                                           pattern="yyyy-MM-dd HH:mm:ss"/>' style="width: 150px"/></td>
                                                <td>
                                                    <form:select path="expBuyPlan.expBuyPlanList[${idx.index}].expStock.sterilizationMethod"  htmlEscape="false" id="mySelects${idx.index}"
                                                         cssStyle="width: 100px" data-parsley-required="true">
                                                        <form:option value="" label=""/>
                                                        <form:options items="${fns:getDictList('STERILIZATION_METHOD')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                                                    </form:select>
                                                </td>
                                                <td><input type="text" name="expBuyPlanList[${idx.index}].expStock.sterilizationBatchNo"
                                                              class="form-control" value="${expBuyPlan.expStock.sterilizationBatchNo}" style="width: 120px"></td>
                                                <td><input name="expBuyPlanList[${idx.index}].expStock.sterilizationDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true"
                                                           readonly onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value='<fmt:formatDate value="${expBuyPlan.expStock.sterilizationDate}"
                                                           pattern="yyyy-MM-dd HH:mm:ss"/>' style="width: 150px"/></td>
                                                <td><input type="text" name="expBuyPlanList[${idx.index}].expProvideApplication.auditQuantity" id="auditQuantity${idx.index}"
                                                              class="form-control" value="${expBuyPlan.expProvideApplication.auditQuantity}" style="width: 60px"
                                                        onchange="changeMoney('auditQuantity','${idx.index}')"></td>
                                                <td>
                                                    <a href="#" onclick="updateForm('${expBuyPlan.expProvideApplication.id}','${idx.index}')" class="btn btn-success btn-xs">修改</a>
                                                    <a href='#' onclick='removeList(this)' class='applyBt btn btn-danger btn-xs'>删除</a>
                                                </td>
                                                <td hidden><input type="hidden" name="expBuyPlanList[${idx.index}].expStock.expImportMaster.importClass"/></td>
                                                <td hidden><input type="hidden" name="expBuyPlanList[${idx.index}].expStock.expImportMaster.supplier"/></td>
                                                <td hidden><input type="hidden" name="expBuyPlanList[${idx.index}].expStock.expImportMaster.accountType"/></td>
                                                <td hidden><input type="hidden" name="expBuyPlanList[${idx.index}].expStock.expExportMaster.exportClass"/></td>
                                                <td hidden><input type="hidden" name="expBuyPlanList[${idx.index}].expStock.expImportMaster.importDate"/></td>
                                            </tr>
                                        </c:forEach>
                                        </tbody>
                                    </table>
                                </form>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
        <%--右侧消耗品规格字典列表--%>
        <section class="main-content col-lg-4" style="display: none" id="stcokRightDiv">
            <div class="content-wrap" id="rigthCenterDiv">
            </div>
        </section>
    </section>
</div>
<script type="text/javascript">
    //设置出库日期为当前时间,不能编辑
    var date = new Date();
    var nowMonth = "";
    if (date.getMonth() + 1 <= 9) {
        nowMonth = "0" + (date.getMonth() + 1) + "";
    } else {
        nowMonth = (date.getMonth() + 1) + "";
    }
    var hours = "";
    if (date.getHours() < 10) {
        hours = "0" + date.getHours();
    } else {
        hours = date.getHours();
    }
    var minutes = "";
    if (date.getMinutes() < 10) {
        minutes = "0" + date.getMinutes();
    } else {
        minutes = date.getMinutes();
    }
    var seconds = "";
    if (date.getSeconds() < 10) {
        seconds = "0" + date.getSeconds();
    } else {
        seconds = date.getSeconds();
    }
    var now = date.getFullYear() + "-" + nowMonth + "-" + date.getDate() + " " + hours + ":" + minutes + ":" + seconds;
    $('#startDate').val(now);

    //对消入出库金额合计
//    $('#totalMoney').val(0.00);
    $("#totalMoney").prop('disabled', 'disabled');

    //提取
    function getBuyPlanList(){
        var purchaseNumber = $('#purchaseNumber').val();
        if($.trim(purchaseNumber) == '' || typeof(purchaseNumber) == 'undefined'){
            toastr.error('请先选择采购单号,才能提取数据!');
            return ;
        }
    }

    loadSupplier();
    function loadSupplier() {
        $("#supplier").html("");  //供货单位
        var importClassValue = $('#importClass').val();  //入库类别 代码
        var importClassName = $('#importClass option:selected').text();//入库类别 名称
        var exportClassValue = $('#exportClass').val();   //出库类别  代码
        var exportClassName = $('#exportClass option:selected').text(); //出库类别  名称
        $.ajax({
            url: '${ctx}/csm/expSupplierCata/getSupplierOrStorageList?inoutType=' + '1' + '&type=' + importClassValue,
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR) {
                if (data.length > 0) {
                    if (typeof(data[0].supplierJc) == 'undefined') {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.storageCode + "'>" + item.storageName + "</option>");
                        });
                    } else {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.id + "'>" + item.supplierJc + "</option>");
                        });
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
        $('#importClass').attr('disabled',true);
        $('#exportClass').attr('disabled',true);
    }

    //弹出右边修改指定行数据
    function updateForm(id,idxIndex){
        $("#stcokLeftDiv").attr("class","main-content col-lg-8");
        $("#stcokRightDiv").show('normal');
        var url = '${ctx}/csm/buyExpPlan/updateForm?expProvideApplication.id=' + id + '&idxIndex=' + idxIndex;
        return loadDiv('rigthCenterDiv',url);
    }

    //删除一行消耗品
    function removeList(btn){
        $(btn).parent().parent().remove();
    }

    function savePlanListForm() {
        var importClass = $('#importClass').val();  //入库类别
        var supplier = $('#supplier').val();  //供货单位
        var startDate = $('#startDate').val();  //日期
        var accountType = $('#accountType').val();  //付款方式
        var exportClass = $('#exportClass').val();  //出库类别
        if(supplier == null || supplier == '' || typeof(supplier) == 'undefined'){
            toastr.error('请选择供货单位!');
            return false;
        }
        if(accountType == null || accountType == '' || typeof(accountType) == 'undefined'){
            toastr.error('采购入库,付款方式不能为空!');
            return false;
        }
        var list=$("table tr").find("td:eq(0)");
        if(list.length > 0){
            for(var i = 0; i < list.length; i++){
                $("#buyPlanBody").find("tr:eq("+ i +")").children().eq(23).find('input').val(importClass);
                $("#buyPlanBody").find("tr:eq("+ i +")").children().eq(24).find('input').val(supplier);
                $("#buyPlanBody").find("tr:eq("+ i +")").children().eq(25).find('input').val(accountType);
                $("#buyPlanBody").find("tr:eq("+ i +")").children().eq(26).find('input').val(exportClass);
                $("#buyPlanBody").find("tr:eq("+ i +")").children().eq(27).find('input').val(startDate);
            }
        }else{
            toastr.error('没有数据需要保存!');
            return false;
        }
        $("#savePlanListForm").ajaxSubmit({
            success: function (data) {
                if (data.code == 'success') {
                    toastr.success(data.data);
                    <%--loadDivForm('mainCenterDiv','searchForm','${ctx}/csm/buyExpPlan/buyPlanImpExpIndex');--%>
                    $('#contentTable tbody').empty();
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error(data.data);
            }
        });
    }

    //改变进货价或数量重新计算合计金额
    function changeMoney(inputId, idx) {
        if (inputId == 'auditQuantity') {  //改变数量
            var auditQuantity = $.trim($("#" + inputId + idx).val());
            var tradePrice = $.trim($("#tradePrice" + idx).val());
            $("#totalMoney").val(new Number(auditQuantity) * new Number(tradePrice));
        }
        if (inputId == 'tradePrice') {  //改变进价
            var tradePrice = $.trim($("#" + inputId + idx).val());
            var auditQuantity = $.trim($("#auditQuantity" + idx).val());
            $("#totalMoney").val(new Number(auditQuantity) * new Number(tradePrice));
        }
    }
</script>